package com.example;

/**
 * @desc:
 * @author: Shoko
 * @date: 2025/6/1
 */
// ChatGroup.java
import java.util.Hashtable;

public class ChatGroup implements AbstractChatroom {
    private Hashtable<String, Member> members = new Hashtable<>();
    private static final int MAX_IMAGE_SIZE = 1024; // 1KB

    @Override
    public void register(Member member) {
        if (!members.containsKey(member.getName())) {
            members.put(member.getName(), member);
            member.setChatroom(this);
        }
    }

    @Override
    public void sendText(String from, String to, String message) {
        Member member = members.get(to);
        if (member != null) {
            String filteredMessage = filterMessage(message);
            member.receiveText(from, filteredMessage);
        }
    }

    @Override
    public void sendImage(String from, String to, String image) {
        Member member = members.get(to);
        if (member != null) {
            if (checkImageSize(image)) {
                member.receiveImage(from, image);
            } else {
                Member sender = members.get(from);
                if (sender != null) {
                    sender.receiveText("System", "图片大小超过限制，发送失败");
                }
            }
        }
    }

    private String filterMessage(String message) {
        return message.replaceAll("糟糕|笨蛋|混蛋", "***");
    }

    private boolean checkImageSize(String image) {
        return image.length() <= MAX_IMAGE_SIZE;
    }
}
